package com.example.cyh.bczapp.Code.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.cyh.bczapp.Code.BLL.UserBLL;
import com.example.cyh.bczapp.Code.Model.UserInfEntity;
import com.example.cyh.bczapp.R;

public class LoginActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        findViewById(R.id.registernow).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                LoginActivity.this.startActivity(intent);
            }
        });
        findViewById(R.id.forgotpassword).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, ForgetActivity.class);
                LoginActivity.this.startActivity(intent);
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        findViewById(R.id.login_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    String id = ((EditText) findViewById(R.id.login_username)).getText().toString();
                    String pwd = ((EditText) findViewById(R.id.login_password)).getText().toString();
                    @Override
                    public void run() {
                        try {
                            Looper.prepare();
                            if (id == null || id.length() <= 0) {
                                Toast.makeText(LoginActivity.this, "账号不能为空", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            if (pwd == null || pwd.length() <= 0) {
                                Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            ///写入文件系统
                            SharedPreferences.Editor editor=getSharedPreferences("Logindata",MODE_PRIVATE).edit();
                            editor.putString("id",id);
                            editor.putString("pwd",pwd);
                            editor.apply();
                            ///写入文件系统
                            UserBLL ub=new UserBLL();
                            UserInfEntity user=ub.Login(id,pwd);
                            if(user!=null) {
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                intent.putExtra("user", user);
                                LoginActivity.this.startActivity(intent);
                                finish();
                            }
                            Toast.makeText(LoginActivity.this,user!=null?"登陆成功":"登陆失败", Toast.LENGTH_SHORT).show();
                        } catch (Exception ex) {
                            Toast.makeText(LoginActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show();
                        } finally {
                            Looper.loop();
                        }
                    }
                }).start();
            }
        });
    }
}
